﻿using System.IO;
using Brilliants.UI;
using System.Windows;
using Microsoft.Win32;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace WpfAi
{
    /// <summary>
    /// Photo.xaml 的交互逻辑
    /// </summary>
    public partial class Photo : NbWindow
    {
        public Photo(ImageSource img)
        {
            InitializeComponent();
            ShowPic(img);
        }
        /// <summary>
        /// 显示图片
        /// </summary>
        /// <param name="img"></param>
        public void ShowPic(ImageSource img)
        {
            this.img.Source = img;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {

            SaveFileDialog SaveFileDialog = new SaveFileDialog();
            SaveFileDialog.Filter = "Image Files (*.bmp, *.png, *.jpg)|*.bmp;*.png;*.jpg | All Files | *.*";
            SaveFileDialog.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录 
            if (SaveFileDialog.ShowDialog() == true)
            {
                var encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create((BitmapSource)this.img.Source));
                using (FileStream stream = new FileStream(SaveFileDialog.FileName, FileMode.Create))
                {
                    encoder.Save(stream);
                    MessageBox.Show(this, "保存成功");
                }
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            //this.DialogResult = false;
            this.Close();
        }

        /// <summary>
        /// 图片左转
        /// </summary>
        /// <param name="img">被操作的前台Image控件</param>
        public void RotateLeft(Image img)
        {
            TransformGroup tg = img.RenderTransform as TransformGroup;
            var tgnew = tg.CloneCurrentValue();
            if (tgnew != null)
            {
                RotateTransform rt = tgnew.Children[2] as RotateTransform;
                img.RenderTransformOrigin = new Point(0.5, 0.5);
                rt.Angle -= 90;
            }

            // 重新给图像赋值Transform变换属性
            img.RenderTransform = tgnew;
        }

        /// <summary>
        /// 图片右转
        /// </summary>
        /// <param name="img">被操作的前台Image控件</param>
        public void RotateRight(Image img)
        {
            TransformGroup tg = img.RenderTransform as TransformGroup;
            var tgnew = tg.CloneCurrentValue();
            if (tgnew != null)
            {
                RotateTransform rt = tgnew.Children[2] as RotateTransform;
                img.RenderTransformOrigin = new Point(0.5, 0.5);
                rt.Angle += 90;
            }

            // 重新给图像赋值Transform变换属性
            img.RenderTransform = tgnew;
        }
        private BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
        {
            BitmapImage bitmapImage = new BitmapImage();
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                bitmap.Save(ms, bitmap.RawFormat);
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = ms;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                bitmapImage.Freeze();
            }
            return bitmapImage;
        }
        private void btnFrash_Click(object sender, RoutedEventArgs e)
        {
            RotateLeft(this.img);
        }
    }
}
